package com.vic.delayedqueue.controller;

import com.vic.delayedqueue.config.DelayedQueue1Config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 利用ttl和死信队列实现的延时队列
 */
@Slf4j
@RestController
@RequestMapping("producer1")
public class Producer1Controller {

    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     * 带有ttl的队列，创建时设置ttl参数
     * @param message
     * @return
     */
    @GetMapping("/ttl/sendMsg/{message}")
    public String sendMsg(@PathVariable String message) {
        log.info("当前时间: {},发送一条信息给两个 TTL 队列: {}", new Date(), message);
        rabbitTemplate.convertAndSend(DelayedQueue1Config.NORMAL_EXCHANGE, DelayedQueue1Config.NORMAL_ROUTING_KEY_1, "消息来自 ttl 为 30S 的队列: " + message);
        rabbitTemplate.convertAndSend(DelayedQueue1Config.NORMAL_EXCHANGE, DelayedQueue1Config.NORMAL_ROUTING_KEY_2, "消息来自 ttl 为 10S 的队列: " + message);
        return "ok";
    }

}
